package cn.learn.controller.cargo;

import cn.learn.base.BaseController;
import cn.learn.domain.cargo.*;
import cn.learn.service.cargo.FinanceService;
import cn.learn.utils.commons.BeanMapUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/cargo/finance")
public class FinanceController extends BaseController {
    @Reference
    private FinanceService financeService;

    @RequestMapping(value = "list", name = "查询财务管理")
    public String list(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4") int pageSize) {
        FinanceExample financeExample = new FinanceExample();
        financeExample.setOrderByClause("create_time desc");
        FinanceExample.Criteria criteria = financeExample.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        PageInfo pb = financeService.finaAll(financeExample, pageNum, pageSize);
        request.setAttribute("pageList", pb);
        return "/cargo/finance/finance-list";
    }

    @RequestMapping(value = "toView", name = "查看财务单")
    public String toView(String id) {
        Finance finance = financeService.findById(id);
        request.setAttribute("finance", finance);
        return "/cargo/finance/finance-view";
    }

    @RequestMapping(value = "toUpdate", name = "到财务单修改页面")
    public String toUpdate(String id) {
        Finance finance = financeService.findById(id);
        request.setAttribute("finance", finance);
        return "/cargo/finance/finance-update";
    }

    @RequestMapping(value = "update", name = "修改财务单")
    public String update(Finance finance) {
        financeService.update(finance);
        return "redirect:/cargo/finance/list.do";
    }

    @RequestMapping(value = "submit", name = "提交财务单")
    public String submit(String id) {
        Finance finance = financeService.findById(id);
        finance.setState(1);
        financeService.update(finance);
        return "redirect:/cargo/finance/list.do";
    }

    @RequestMapping(value = "cancel", name = "取消提交财务单")
    public String cancel(String id) {
        Finance finance = financeService.findById(id);
        finance.setState(0);
        financeService.update(finance);
        return "redirect:/cargo/finance/list.do";
    }

    @RequestMapping(value = "delete", name = "删除财务单")
    public String delete(String id) {
        financeService.delete(id);
        return "redirect:/cargo/finance/list.do";
    }

    @RequestMapping(value = "toPrint", name = "打印财务单")
    public void toPrint(String id) throws Exception {
        // 根据财务单的id获取财务单
        Finance finance = financeService.findById(id);
        // 动态导入财务单  map--$P
        Map<String, Object> map = BeanMapUtils.beanToMap(finance);
        for (String s : map.keySet()) {
            System.out.println(s.toCharArray());
            //System.out.println(s.toString());
        }
        /*HashMap map = new HashMap();
        map.put("inputDate",finance.getInputDate());
        map.put("portOfLoading",finance.getPortOfLoading());
        map.put("portOfDischarge",finance.getPortOfDischarge());
        map.put("loadingdate",finance.getLoadingdate());
        map.put("invoicemoney",finance.getInvoicemoney());
        map.put("createBy",finance.getCreateBy());
        map.put("limitdate",finance.getLimitdate());
        map.put("copynum",finance.getCopynum());

        for (Object o : map.keySet()) {
            System.out.println(o.toString());
        }*/

        // 读取模板动态填充
        String path = session.getServletContext().getRealPath("/jasperPDF/test1.jasper");
        FileInputStream file = new FileInputStream(path);
        JREmptyDataSource jrEmptyDataSource = new JREmptyDataSource();
        //Map map = new HashMap();
        JasperPrint jasperPrint = JasperFillManager.fillReport(file, map, jrEmptyDataSource);
        // 导出
        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
    }
}
